package com.jiangcheng.demo.controller;


import com.jiangcheng.demo.entity.Article;
import com.jiangcheng.demo.service.ArticleService;
import com.jiangcheng.demo.utils.Result;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-07-18
 */
@Api("文章相关API")
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @GetMapping("/findArticleByPage/{page}/{pageSize}/{filterUId}")
    public Result findArticleByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize, @PathVariable("filterUId") Integer filterUId) {
        System.out.println("页数:" + page + " 数量: " + pageSize);
        System.out.println("过滤的用户是:  " + filterUId);
        return articleService.findArticleByPage(page, pageSize, filterUId);
    }

    @GetMapping("/findArticleByAuthorId/{authorId}")
    public Result findArticleByAuthorId(@PathVariable("authorId") int authorId) {
        System.out.println("controller:" + authorId);
        return articleService.findArticleByAuthorId(authorId);
    }

    // 根据传入字符串对文章标题即内容进行模糊查询
    @GetMapping("/searchArticle/{search}")
    public Result findArticleByLike(@PathVariable("search") String search) {
        return articleService.findArticleByLike(search);
    }

    // 文章插入 @Param obj(title,content,authorId)
    @PostMapping("/articleWrite")
    public Result ArticleWrite(@Param("article") Article article) {
        System.out.println("插入的article:" + article);
        return articleService.InsertArticle(article);
    }

    // 修改文章
    @PostMapping("/updateArticle")
    public Result UpdateArticle(Article article) {
        System.out.println(article);
        return articleService.UpdateArticle(article);
    }

    // 文章详情
    @GetMapping("/articleDetails/{artId}")
    public Result articleDetails(@PathVariable("artId") int artId) {
        return articleService.findArticleInfoById(artId);
    }

    // 删除文章
    @PostMapping("/deleteArtcle")
    public Result delete(@Param("artId") int artId) {
        return articleService.deleteArticleById(artId);
    }

//    // 查询文章
    @GetMapping("/findArticle/{artId}")
    public Result findArticle(@PathVariable("artId") int artId) {
        return articleService.findArticleById(artId);
    }


}

